/**
 * COPYRIGHT HangZhou Trustway Technology Company Limited
 * All right reserved.
 */
package com.robam.cloud.usercenter.helper;

import com.robam.cloud.common.bean.apicenter.entity.SysConfigWithBLOBs;
import com.robam.cloud.common.bean.common.Constant;
import com.robam.cloud.common.bean.common.SysConfigConstant;
import com.robam.cloud.common.bean.restful.entity.ServiceException;
import com.robam.cloud.common.bean.usercenter.entity.LoginAccount;
import com.robam.cloud.common.bean.usercenter.entity.User;
import com.robam.cloud.common.bean.usercenter.enumspace.UserStatusEnum;
import com.robam.cloud.restful.config.initdata.SysConfigData;
import com.robam.cloud.usercenter.dao.LoginAccountMapper;
import com.robam.cloud.usercenter.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Description
 * @Author 薛铁琪
 * @CreateTime 2020-07-23 12:54
 * @Version 1.0
 */
@Component
public class UserHelper {
    @Autowired
    UserMapper userMapper;
    @Autowired
    LoginAccountMapper loginAccountMapper;
    @Autowired
    SysConfigData sysConfigData;

    /**
     * 初始化user 新的插入 老的更新
     *
     * @param user
     * @return
     */
    public User initUser(User user) {
        Long isNewUser = userMapper.isNewUser(user.getSfzmhm());
        if (isNewUser != null && isNewUser > 0) {
            User oldUser = userMapper.getOldUser(user.getSfzmhm());
            //更新手机号码 姓名
            User updateUser = new User();
            updateUser.setId(oldUser.getId());
            if (user.getSjhm() != null && (oldUser.getSjhm() == null || (!user.getSjhm().equals(oldUser.getSjhm())))) {
                updateUser.setSjhm(user.getSjhm());
            }
            if (user.getXm() != null && (oldUser.getXm() == null || (!user.getXm().equals(oldUser.getXm())))) {
                updateUser.setXm(user.getXm());
            }
            if (updateUser.getXm() != null || updateUser.getSjhm() != null) {
                userMapper.updateByPrimaryKeySelective(updateUser);
            }
            return userMapper.selectByPrimaryKey(oldUser.getId());
        } else {
            user.setStatus(UserStatusEnum.NORMAL.getCode());
            user.setZt(Constant.LOGICNORMAL);
            user.setZcsj(new Date());
            userMapper.insertSelective(user);
            return user;
        }
    }

    /**
     * 原来实名过，进来是不同的实名信息
     *
     * @param loginAccount
     * @param user
     */
    public void exchangeUser(LoginAccount loginAccount, User user) {
        SysConfigWithBLOBs sysConfigByCsdm = sysConfigData.getSysConfigByCsz(SysConfigConstant.SMDTCL);
        if (sysConfigByCsdm == null) {
            throw new ServiceException("未配置实名替换策略规则");
        }
        String exchangeUserModul = sysConfigByCsdm.getCsz();
        if ("1".equals(exchangeUserModul)) {
            User initUserBack = initUser(user);
            loginAccount.setUserId(initUserBack.getId());
            loginAccount.setRzsj(new Date());
            loginAccountMapper.updateByPrimaryKeySelective(loginAccount);
        } else if ("2".equals(exchangeUserModul)) {
            throw new ServiceException("不允许替换实名信息");
        } else {
            throw new ServiceException("不支持的实名替换模式");
        }
    }

    /**
     * 判断是否实名
     *
     * @param loginAccountId
     * @return
     */
    public Boolean realVerify(Long loginAccountId) {
        LoginAccount loginAccount = loginAccountMapper.selectByPrimaryKey(loginAccountId);
        if (loginAccount == null || Constant.LOGICDELETE.equals(loginAccount.getZt())) {
            return false;
        } else {
            if (loginAccount.getUserId() == null) {
                return false;
            } else {
                User user = userMapper.selectByPrimaryKey(loginAccount.getUserId());
                if (user == null || Constant.LOGICDELETE.equals(user.getZt())) {
                    return false;
                } else {
                    return true;
                }
            }
        }
    }

}
